/****************************************************************************************
*	Mouzetrap - client/server framework	
*
*	$HeadURL: http://mouzetrap.googlecode.com/svn/trunk/Mouzetrap/DataStructures/LockfreeStack.cs $
*	$Id: LockfreeStack.cs 11 2009-06-11 09:10:25Z tobz@fallenrealms.net $
*
*	Original copyright Julian M Bucknall (www.boyet.com) 
* 
*	Copyright (c) 2008 Toby Lawrence
* 
*	Permission is hereby granted, free of charge, to any person obtaining a copy
*	of this software and associated documentation files (the "Software"), to deal
*	in the Software without restriction, including without limitation the rights
*	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*	copies of the Software, and to permit persons to whom the Software is
*	furnished to do so, subject to the following conditions:
* 
*	The above copyright notice and this permission notice shall be included in
*	all copies or substantial portions of the Software.
* 
*	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*	THE SOFTWARE.
* 
****************************************************************************************/

namespace Mouzetrap.DataStructures
{
	/// <summary>
	/// Represents a variable size last-in-first-out (LIFO) collection of instances of the same arbitrary type.
	/// </summary>
	/// <typeparam name="T">the type of the elements in the stack</typeparam>
	/// <remarks>This collection is thread-safe.  Synchronization is provided via interlocked operations.</remarks>
	public class LockfreeStack<T>
	{
		private readonly ItemNode<T> _head;

		/// <summary>
		/// Creates a new lock-free stack, using the default dequeueing strategy.
		/// </summary>
		public LockfreeStack()
		{
			_head = new ItemNode<T>();
		}

		/// <summary>
		/// Pushes an item onto the front of the stack.
		/// </summary>
		/// <param name="item">the item to push on the stack</param>
		public void Push(T item)
		{
			var newNode = new ItemNode<T>(item);

			do
			{
				newNode.Next = _head.Next;
			} while(!SyncUtilities.CompareAndSwap(ref _head.Next, newNode.Next, newNode));
		}

		/// <summary>
		/// Pops an item off of the front of the stack.
		/// </summary>
		/// <param name="item">the popped item</param>
		/// <returns>
		/// the first item on the front of the stack, or the default value for the element type if 
		/// the stack was empty.
		/// </returns>
		public bool Pop(out T item)
		{
			ItemNode<T> node;

			do
			{
				node = _head.Next;

				if(node == null)
				{
					item = default(T);

					return false;
				}
			} while(!SyncUtilities.CompareAndSwap(ref _head.Next, node, node.Next));

			item = node.Item;

			return true;
		}

		/// <summary>
		/// Pops an item off of the front of the stack.
		/// </summary>
		/// <returns>the first item on the front of the stack</returns>
		public T Pop()
		{
			T result;
			Pop(out result);

			return result;
		}
	}
}